package com.geralt.im.commands.impl;

import java.util.HashSet;

import com.geralt.im.command.Command;
import com.geralt.im.commandenum.Code;
import com.geralt.im.model.Message;
import com.geralt.im.protocol.Response;
import com.geralt.im.server.IMServer;

public class ExitCommand extends Command {
	@Override
	public Response excute() {
		String id = (String) parameter.get("EXIT");
		boolean validation = (id!=null)&&(IMServer.map.containsKey(id));
		receivers.clear();
		if (validation) {
			if(session!=null)
				session.setRun(false);
			Response response = new Response();
			response.setStatusCode(Code.OK);
			response.setType("LIST");
			IMServer.map.remove(id);
			HashSet<String> hashSet = new HashSet<>(IMServer.map.keySet());
			response.getParameter().put("MAP",
					hashSet);
			receivers.addAll(hashSet);
			return response;
		} else {
			session.setRun(false);
			Response response = new Response();		
			response.setStatusCode(Code.BAD_REQUEST);
			response.setType("SEND");
			Message msg = new Message();
			msg.setContent("");
			response.getParameter().put("MSG",msg);
			return response;
		}
	}

}
